package com.ggh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ggh.config.annotation.SysLog;
import com.ggh.entity.User;
import com.ggh.service.UserService;
import com.ggh.util.UploadFileTool;
import com.ggh.vo.Body;
import com.ggh.vo.Meta;
import com.ggh.vo.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jj
 * @date 2019/10/19 15:58
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * @description :查询是否有绑定
     * @return: com.ggh.vo.UserToken
     * @date : 2019/10/17 9:59
     * @author : jj
     * @throws
     */
    @RequestMapping(value = "/getbyid",method = RequestMethod.POST)
    @ResponseBody
    public Body getByid(Integer  userid){
        User user = userService.getById(userid);
      return Body.newInstance(user);
    }

    @GetMapping("/list")
    @SysLog("跳转系统用户列表页面")
    public String list(){
        return "admin/user/list";
    }
    @ResponseBody
    @PostMapping("/findlist")
    @SysLog("查询用户列表")
    public PageInfo findAlllist(int page,int limit){
        IPage<User> pageData=userService.page(new Page<User>(page,limit), new QueryWrapper<>(null));
        PageInfo ipage = new PageInfo();
        ipage.setCode(0);
        ipage.setCount(pageData.getTotal());
        ipage.setData(pageData.getRecords());
        ipage.setMsg("用户信息");
        return ipage;
    }

    @GetMapping("edit")
    @SysLog("编辑个人信息" )
    public String edit(Integer userid, ModelMap modelMap){
        User user = userService.getById(userid);
        modelMap.put("localuser",user);
        return "admin/user/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存系统用户编辑数据")
    public Body edit(@RequestBody  User user){
    return userService.edit(user);
    }
    @SysLog("修改密码跳转")
    @GetMapping("changePassword")
    public String changePassword(ModelMap modelMap){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        modelMap.put("currentUser",userService.getById(user.getUserid()));
        return "admin/user/changePassword";
    }

    @SysLog("用户修改密码")
    @PostMapping("changePassword")
    @ResponseBody
    public Body changePassword(@RequestParam(value = "oldPwd",required = false)String oldPwd,
                                         @RequestParam(value = "newPwd",required = false)String newPwd,
                                         @RequestParam(value = "confirmPwd",required = false)String confirmPwd){
        return userService.changePassword(oldPwd,newPwd,confirmPwd);
    }
    @GetMapping("userinfo")
    public String toEditMyInfo(ModelMap modelMap){
        User olduser = (User) SecurityUtils.getSubject().getPrincipal();
        User user = userService.getById(olduser.getUserid());
        modelMap.put("userinfo",user);
        return "admin/user/userInfo";
    }

    @SysLog("系统用户个人信息修改")
    @PostMapping("saveUserinfo")
    @ResponseBody
    public Body saveUserInfo(Integer userid,String loginName,String phone,String avatarurl,String nickname){
        return userService.saveUserInfo(userid,loginName,phone,avatarurl,nickname);
    }
    @SysLog("上传头像")
    @PostMapping("uploadFace")
    @ResponseBody
    public Body uploadFile(@RequestParam("icon") MultipartFile file, HttpServletRequest httpServletRequest) {
        if(file == null){
            return Body.newInstance(Meta.CODE_500,"上传文件为空 ");
        }
        Map map = new HashMap();
        try {
            String url = UploadFileTool.uploadFile(httpServletRequest, file);
            map.put("url", url);
            map.put("name", file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
            return  Body.newInstance(e.getMessage());
        }
        return Body.newInstance(map);
    }
}
